#ifndef BCITEST_H
#define BCITEST_H

#include <QBciAnalyzer>
#include <QBciApp>
#include <QBciSource>

#include <QThread>

using namespace bci;
class BciTestSource : public QBciSource {
  Q_OBJECT
public:
  explicit BciTestSource(const QString &name) : QBciSource(name) {
    setRunType(Loop);
  }

protected slots:
  void run() override {
    arma::wall_clock clk;
    clk.tic();

    info(this->objectName() + "::work on thread: " +
         QBciDebug::threadId2QStr(QThread::currentThreadId()));
    info(this->objectName() + "::reading data: ");
    for (int count = 0; count < 10; ++count) {
      while (clk.toc() < 0.15) {
      }
      std::cout << ".";
      std::cout << std::flush;
      clk.tic();
    }
    std::cout << std::endl;
    emit sendData(QBciSignal());
  }
};

class BciTestAnalysis : public QBciAnalyzer {
  Q_OBJECT
public:
  explicit BciTestAnalysis(const QString &name) : QBciAnalyzer(name) {}

protected slots:
  virtual void recvData(QBciSignal signal) override {
    info(this->objectName() + "::work on thread: " +
         QBciDebug::threadId2QStr(QThread::currentThreadId()));
    info(this->objectName() + "::process data: ");
    QBciRes res;
    res.insert("test", QVariant(1));
    emit sendRes(res);
  }
};

class BciTestUser : public QBciApp {
  Q_OBJECT
public:
  explicit BciTestUser(const QString &name) : QBciApp(name) {}

signals:
  void sendResultTcp(const QByteArray &bytes);

protected slots:
  void recvRes(const ::bci::QBciRes &result) override {

    info(this->objectName() + "::work on thread: " +
         QBciDebug::threadId2QStr(QThread::currentThreadId()));
    info(this->objectName() + "::receive result");

    QString sendMessage;
    QByteArray data = sendMessage.toUtf8();
    std::cout << data.toStdString() << std::endl;
    emit sendResultTcp(data);
  }

private:
};

#endif // BCITEST_H
